package com.lancoo.znbkxx.teacher.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.Type
import kotlinx.android.synthetic.main.item_comment_questiontype.view.*
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @author : Admin
 * @date   : 2019/7/17 11:14
 * @desc   :
 */
class QuestionTypeAdapter(layoutResId: Int, data: MutableList<Type>?) :
    BaseQuickAdapter<Type, BaseViewHolder>(layoutResId, data) {
    override fun convert(helper: BaseViewHolder, item: Type?) {
        val position = helper.layoutPosition
        val totalCount = getTotalCount()
        with(helper.itemView) {
            tv_questiontype?.text =
                if (item!!.GenreName.isBlank()) item.TypeName else item.GenreName
            tv_read_num?.text = item.AvgScoreRate.times(100)
                .let { "${BigDecimal(it).setScale(2, RoundingMode.HALF_UP)}" + "%" }
            tv_percent_num.text = item.Count.div(totalCount.toDouble()).times(100)
                .let { "${BigDecimal(it).setScale(2, RoundingMode.HALF_UP)}" + "%" }
            cl.setBackgroundResource(
                when (position % 3) {
                    0 -> R.mipmap.questype_bg_blue
                    1 -> R.mipmap.questype_bg_yellow
                    else -> R.mipmap.questype_bg_red
                }
            )
        }
    }

    /*
    题型总数量
     */
    private fun getTotalCount(): Int {
        var count = 0
        for (i in data.indices) {
            count += data[i].Count
        }
        return count
    }
}